﻿using LyData.Data;
using System;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_FriendLinkAdd : System.Web.UI.Page
{
    protected void Add_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            string path = "";
            if (this.Logo.HasFile)
            {
                path = string.Format("{0}FriendLink/{1}", FSO.GetUploadPath(), FSO.RandomFileName(this.Logo.FileName));
                string filePath = base.Server.MapPath(path);
                FSO.PathExists(filePath);
                this.Logo.SaveAs(filePath);
            }
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string[,] includeColumns = new string[,] { { "Title", this.FriendLinkTitle.Text, "NVarChar", "32" }, { "Type", this.TypeList.SelectedValue, "bit", "0" }, { "Url", this.Url.Text, "NVarChar", "100" }, { "friendLinkDescribe", this.friendLinkDescribe.Text, "NVarChar", "100" }, { "LogoPath", path, "NVarChar", "100" }, { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
            db.Insert("FriendLink", includeColumns);
            db.Close();
            this.Msg.Text = "友情链接添加成功！";
            this.Msg.Visible = true;
            this.Url.Text = "http://";
            this.FriendLinkTitle.Text = "";
            this.friendLinkDescribe.Text = "";
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if ((this.TypeList.SelectedValue.ToLower() == "false") & (this.FriendLinkTitle.Text == ""))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void LogoCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.Logo.HasFile)
        {
            string extension = FSO.GetExtension(this.Logo.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.LogoCustomValidator.ErrorMessage = "Logo格式错误，合法格式：" + extensionList;
        }
        else if (this.TypeList.SelectedValue.ToLower() == "true")
        {
            this.LogoCustomValidator.ErrorMessage = "Logo路径选择错误！";
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("C_FriendLinkAdd.aspx"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Msg.Visible = true;
            this.Add.Enabled = false;
        }
        else
        {
            this.Msg.Visible = false;
            for (int i = 0; i < this.TypeList.Items.Count; i++)
            {
                this.TypeList.Items[i].Attributes.Add("onclick", "radioFriendLink(this)");
            }
        }
    }
}
